<!-- 首页的：商品分类导航组件 -->
<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseleave="mouseleaveShow()">
        <h2 class="all" @mouseenter="mouseenterShow()">全部商品分类</h2>
        <!-- 三级分类 -->
        <!-- show :控制在search页面的三级分类部分的显示与隐藏 -->
        <!-- 动画效果 -->
        <transition name="sort">
          <div class="sort" v-show="show">
            <!-- 利用事件委派+编程式导航实现路由的跳转与传递参数 -->
            <div class="all-sort-list2" @click="goSeach">
              <!-- 第一层:要包裹二,三层 -->
              <div class="item bo" v-for="(firstItem, index) in categoryList" :key="firstItem.categoryId"
                :class="{ 'itemBgcolor': currentIndex == index }">
                <h3 @mouseenter="changeIndex(index)">
                  <a :data-categoryName="firstItem.categoryName" :data-category1Id="firstItem.categoryId">{{
                    firstItem.categoryName }}</a>
                </h3>
                <div class="item-list clearfix" :style="{ display: currentIndex == index ? 'block' : 'none' }">
                  <!-- 第二层:要包裹第三层 -->
                  <div class="subitem" v-for="secondItem in firstItem.categoryChild" :key="secondItem.categoryId">
                    <dl class="fore">
                      <dt>
                        <a :data-categoryName="secondItem.categoryName" :data-category2Id="secondItem.categoryId">{{
                          secondItem.categoryName }}</a>
                      </dt>
                      <dd>
                        <!-- 第三层 -->
                        <em v-for="thirdItem in secondItem.categoryChild" :key="thirdItem.categoryId">
                          <a :data-categoryName="thirdItem.categoryName" :data-category3Id="thirdItem.categoryId">{{
                            thirdItem.categoryName }}</a>
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>

    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import throttle from "lodash/throttle"
export default {
  name: 'TypeNav',
  data() {
    return {
      currentIndex: -1,
      show: true
    }
  },
  computed: {
    ...mapState('typeNav', ['categoryList'])
  },
  methods: {
    ...mapActions('typeNav', ['setActCategoryList']),
    //改变索引存放当前悬停的一级分类的索引值
    changeIndex(index) {
      this.currentIndex = index
      //console.log('鼠标经过' + index);
    },
    //跳转search页面
    goSeach(e) {
      console.log("打印一下e:");
      console.log(e.target.dataset);
      //解构：节点有一个dataset属性，可以获取节点的自定义属性与属性值
      const { categoryname, category1id, category2id, category3id } = e.target.dataset
      //如果点击的标签身上拥有 categoryname 一定是a标签
      if (categoryname) {
        //整理路由跳转的参数
        let query = { categoryName: categoryname }
        //跳转的search页面
        const location = { name: 'search' }
        //一级分类category1id，二级分类category2id，三级分类category3id的a标签
        if (category1id) {
          query.category1Id = category1id
        }
        if (category2id) {
          query.category2Id = category2id
        }
        if (category3id) {
          query.category3Id = category3id
        }
        //整理完参数
        location.query = query
        // 🚫 避免重复跳转：如果当前路由的 query 和目标 query 完全一样，就 return
        // ✅ 判断当前路由的 query 是否和将要跳转的一样
        const isSameRoute = Object.keys(query).every(key => this.$route.query[key] === query[key]) &&
          Object.keys(this.$route.query).every(key => query[key] === this.$route.query[key])
        if (!isSameRoute) {
          //编程式导航：路由跳转
          this.$router.push(location)
        }
      }
    },
    //判断商品分类显示
    mouseenterShow() {
      if (this.$route.path !== '/home') {
        this.show = true
      }
    },
    //判断商品分类隐藏
    mouseleaveShow() {
      this.currentIndex = -1
      if (this.$route.path !== '/home') {
        this.show = false
      }
    }
  },
  created() {
    // 用节流函数替换 changeIndex 方法本身
    this.changeIndex = throttle(this.changeIndex, 10);
  },
  /*  async mounted() {
     //await this.$store.dispatch('home/setBannerList')
     /* 注意： 把这个setActCategoryList（） 方法获取导航数据移到了App.vue那里，提升了性能，不用每次刷新都重新发请求获取数据*/
  // await this.setActCategoryList()
  /*  console.log(`!!`);
   this.$nextTick(() => {
     console.log(this.categoryList[0]);
     console.log(`!!`);
   })
 } 
} */
}
</script>
<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 28px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          //控制二三级分类的显示与隐藏
          /* &:hover {
            .item-list {
              display: block;
            }
          } */
        }

        .itemBgcolor {
          background-color: skyblue;
        }
      }
    }

    //过渡动画样式

    //过渡样式开始状态
    .sort-enter {
      height: 0px;
      //transform: rotate(0deg);
    }

    //过渡样式结束状态
    .sort-enter-to {
      height: 461px;
      //实现翻转效果 transform: rotate(0deg);-》 transform: rotate(720deg);
      //transform: rotate(360deg);
    }

    //定义动画时间，速率
    .sort-enter-active {
      transition: all .5s linear;
    }
  }
}
</style>
